package com.jinlejia.feedback.question.http;

import com.alibaba.fastjson.JSONArray;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.feedback.common.FbApiConfig;
import com.jinlejia.feedback.common.FbDataFilter;
import com.jinlejia.feedback.databinding.FbActivityQuestionAllProgressBinding;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 *  全部跟进
 */

public class QuestionAllProgressHelper {
    private FbActivityQuestionAllProgressBinding binding;

    public QuestionAllProgressHelper(FbActivityQuestionAllProgressBinding binding) {
        this.binding = binding;
    }

    //region 全部跟进
    public void listTaskFollow(String taskCode, final ListTaskFollowCallBack callBack){
        OkHttpUtils.get()
                .addParams("taskCode",taskCode)
                .url(FbApiConfig.ListTaskFollow)
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        int code = FbDataFilter.code(response);
                        if (code==200){
                            ArrayList<Map<String,Object>> result = new ArrayList<>();
                            JSONArray data = FbDataFilter.dataArray(response);
                            if (data==null || data.isEmpty()){
                                callBack.onEmpty();
                                return;
                            }
                            for (int i = 0; i < data.size(); i++) {
                                Map<String,Object> followMap = new HashMap<>();
                                Map<String,Object> map = data.getJSONObject(i);
                                followMap.put("followCode",String.valueOf(map.get("followCode"))) ;//跟进编码
                                followMap.put("followContent",String.valueOf(map.get("followContent"))); //跟进内容
                                followMap.put("followUserName",String.valueOf(map.get("followUserName")));//跟进人名称
                                followMap.put("followTime",timeFormat(map.get("followTime"))); //跟进时间
                                JSONArray fileList = (JSONArray) map.get("taskFollowFileList");
                                ArrayList<Map<String,String>> files = new ArrayList<>();
                                if (fileList!=null){
                                    for (int j = 0; j < fileList.size(); j++) {
                                        Map<String,Object> file = fileList.getJSONObject(j);
                                        Map<String,String> f = new HashMap<>();
                                        f.put("fileName",String.valueOf(file.get("followFileName")));
                                        f.put("fileUrl",FbApiConfig.MAGEHEADER_URL+String.valueOf(file.get("followFileUrl")));
                                        files.add(f);
                                    }
                                }
                                followMap.put("fileList",files);
                                result.add(followMap);
                            }
                            callBack.onResult(result);
                        }else {
                            callBack.onError();
                        }
                    }
                });
    }

    private String timeFormat(Object time){
        String timeStr = String.valueOf(time);
        if ("null".equalsIgnoreCase(timeStr)){
            return "";
        }
        return timeStr;
    }

    public interface ListTaskFollowCallBack{
        void onResult(ArrayList<Map<String, Object>> result);
        void onError();
        void onEmpty();
    }
    //endregion


}
